#delimit;
** REPLACE FILE PATH WITH PATH TO RELEVANT REPLICATION FILES;
local fileloc = "~/KMS_REPLICATION";
set logtype text;
capture log close zip_level_weather;

log using `fileloc'/log_files/zip_level_weather.txt, name(zip_level_weather) replace;
set more off;
clear all;

**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
**XXXXXXXXXXXXXXXXXXX BUILDING ZIP CODE WEATHER VALUES XXXXXXXXXXXXXXXXXXX
**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;

** Use cutoff of 20 miles (see text);		
use `fileloc'/data/location_data/weather_to_zip_distance.dta, clear;

destring usaf, replace;
destring wban, replace;
	
drop if distance > 20;
		
joinby usaf wban using `fileloc'/data/weather_data/weekly_weather_KMS;
		
** Inverse distance weighting;		
gen weight = 1/distance;

collapse rain humidS max_temp windspeed (max) days_with_rain days_with_fog [w=weight], by(mother_zip week) fast;
		
sort mother_zip week;

sum *;

** Rounding values for simplicity;
replace rain = round(rain,0.01);
replace humidS = round(humidS,0.1);
replace max_temp = round(max_temp,1);
replace windspeed = round(windspeed,0.1);
				
save `fileloc'/data/weather_data/20mile_weather_KMS.dta, replace;

log close zip_level_weather;

